package com.zscat.mallplus.market.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.annotation.SysLog;
import com.zscat.mallplus.sms.entity.SmsCouponHistory;
import com.zscat.mallplus.sms.service.ISmsCouponHistoryService;
import com.zscat.mallplus.sms.service.ISmsCouponService;
import com.zscat.mallplus.ums.entity.UmsMember;
import com.zscat.mallplus.ums.service.IUmsMemberService;
import com.zscat.mallplus.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: rwz
 * @Date: 2020/09/18/12:08 上午
 * @Description:
 */
@Slf4j
@RestController
@Api(tags = "MarketCouponController", description = "优惠券")
@RequestMapping("/api/market/coupon")
public class MarketCouponController {

	@Resource
	private ISmsCouponService couponService;

	@Resource
	private ISmsCouponHistoryService couponHistoryService;

	@Resource
	private IUmsMemberService memberService;

	@IgnoreAuth
	@ApiOperation("优惠券列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
			@ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10") })
	@SysLog(MODULE = "market", REMARK = "优惠券列表")
	@GetMapping(value = "list")
	public Object list(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
			@RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
			@RequestParam(value = "storeId", required = true) Long storeId) {
		UmsMember umsMember = memberService.getNewCurrentMember();
		return new CommonResult()
				.success(couponService.selectCouponList(umsMember.getId(), pageNum, pageSize, storeId));
	}

	@IgnoreAuth
	@ApiOperation("用户优惠券列表")
	@ApiImplicitParam(name = "useStatus", value = "优惠券筛选类型:0->未使用；1->已使用；2->已过期", allowableValues = "0,1,2", dataType = "integer", paramType = "query")
	@SysLog(MODULE = "market", REMARK = "用户优惠券列表")
	@GetMapping(value = "memberCouponList")
	public Object memberCouponList(@RequestParam(value = "useStatus", required = false) Integer useStatus,
			                       @RequestParam(value = "storeId", required = true) Long storeId) {
		List<SmsCouponHistory> couponHistoryList = couponService.listMemberCoupon(useStatus, storeId);
		return new CommonResult().success(couponHistoryList);
	}

	@ApiOperation("用户已领取优惠券列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "payAmount", value = "应付金额", required = false, dataType = "double", paramType = "query"),
			@ApiImplicitParam(name = "storeId", value = "科技馆ID", required = false, dataType = "storeId", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "当前页", required = false, dataType = "int", paramType = "query", defaultValue = "1"),
			@ApiImplicitParam(name = "pageSize", value = "每页显示条数", required = false, dataType = "int", paramType = "query", defaultValue = "10") })
	@SysLog(MODULE = "market", REMARK = "用户优惠券列表")
	@GetMapping(value = "receivedCouponList")
	public Object receivedCouponList(@RequestParam(value = "payAmount", required = false) Double payAmount,
									 @RequestParam(value = "storeId", required = false) Long storeId,
									 @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
									 @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
		UmsMember currentMember = memberService.getNewCurrentMember();
		if (null == currentMember) {
			return new CommonResult().failed("请先登录");
		}

		Date now = new Date();
		return new CommonResult().success(couponHistoryService.page(new Page<SmsCouponHistory>(pageNum, pageSize),
				new QueryWrapper<SmsCouponHistory>().eq("member_id", currentMember.getId()).eq("store_id", storeId)
						.eq("use_status", 0).le("start_time", now)
						.ge("end_time", now).le("min_point", payAmount).orderByDesc("create_time")));
	}

	@IgnoreAuth
	@ApiOperation("领取优惠券")
	@ApiImplicitParam(name = "couponId", value = "优惠券编号", required = true, dataType = "long", paramType = "query")
	@SysLog(MODULE = "market", REMARK = "领取优惠券")
	@GetMapping(value = "receive")
	public Object add(@RequestParam(value = "couponId", required = true) Long couponId) {
		try {
			return couponService.add(couponId);
		} catch (Exception e) {
			return new CommonResult().failed(e.getMessage());
		}
	}
}
